package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行DQL语句
 */
public class JDBCDemo04 {
    public static void main(String[] args) {
        /*
            查看所有老师的id,姓名,性别,年龄,工资
         */


        try (
                //AutoCloseable 可以放在try的()内,即可自动关闭
                Connection connection =  DBUtil.getConnection()
                ){
            System.out.println("数据库连接成功");
            Statement statement = connection.createStatement();
            //SELECT id,name,gender,age,salary FROM teacher;
            String sql = "SELECT id,name,gender,age,salary FROM teacher;";
            ResultSet rs = statement.executeQuery(sql);
            System.out.println("开始查询:");
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                int age = rs.getInt("age");
                int salary = rs.getInt("salary");
                System.out.println(id+","+name+","+gender+","+age+","+salary);
            }
            System.out.println("查询完成");
        } catch (SQLException e) {
            e.printStackTrace();
        }



    }
}
